package view;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;

import com.toedter.calendar.JDateChooser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import controller.Sistema;
import model.Consorcio;
import model.Departamento;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JRegistrarReclamo extends javax.swing.JFrame {
	private JLabel jLabel1;
	private JComboBox jComboConsorcios;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JRadioButton jRadioButton3;
	private JRadioButton jRadioButton2;
	private JRadioButton jRadioButton1;
	private JPanel jPanel1;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private JEditorPane jEditorPane1;
	private JComboBox jComboUF;
	private JPanel jPanel1Panel;
	private static JRegistrarReclamo instancia;
	private JDateChooser calendario;

	//combos
	private Vector<Consorcio> consorcios;
	private Vector<Departamento> departamentos;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JRegistrarReclamo inst = new JRegistrarReclamo();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	private void limpiaPantalla(){
		jEditorPane1.setText("");
	}
	
	private String getUrgenciaSeleccionada(){
		
		if (jRadioButton1.isSelected())
			return "Alta";
			else
				if(jRadioButton2.isSelected())
					return "Media";
				else
					return "Baja";
			
	}
	
	public static JRegistrarReclamo getInstancia(){
		if(instancia==null)
			instancia=new JRegistrarReclamo();
		return instancia;
	}
	
	public JRegistrarReclamo() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Registrar reclamo");
			this.setResizable(false);
			this.setUndecorated(false);
			//this.setVisible(false);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Consorcio");
				jLabel1.setBounds(30, 17, 89, 16);
				jLabel1.setHorizontalTextPosition(SwingConstants.LEADING);
				jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				consorcios = Sistema.getInstancia().getConsorcios();
				//Armar texto							
				Vector<String> nombres = new Vector<String>();
				if(consorcios!=null){
				for (int i=0;i<consorcios.size();i++){
					nombres.add(consorcios.elementAt(i).getNombre());
				}
				}
				ComboBoxModel jComboConsorciosModel = new DefaultComboBoxModel(nombres);
				jComboConsorcios = new JComboBox();
				getContentPane().add(jComboConsorcios);
				jComboConsorcios.setModel(jComboConsorciosModel);
				jComboConsorcios.setBounds(126, 14, 284, 23);
				jComboConsorcios.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());	
						Vector<String> unidades = new Vector<String>();
						if (departamentos != null){
							for (int i=0;i<departamentos.size();i++){
								unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
							}
							jComboUF.removeAllItems();
							for(int i=0; i<unidades.size();i++){
								jComboUF.addItem(unidades.elementAt(i));
								}
							
						}
						
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Unidad funcional");
				jLabel2.setBounds(0, 53, 119, 16);
				jLabel2.setHorizontalTextPosition(SwingConstants.LEADING);
				jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				
				departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());
				//Armar texto
				Vector<String> unidades = new Vector<String>();
				if (departamentos != null){
					for (int i=0;i<departamentos.size();i++){
						unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
					}
					
				}
								
				ComboBoxModel jComboUFModel = new DefaultComboBoxModel(unidades);
				jComboUF = new JComboBox();
				getContentPane().add(jComboUF);
				jComboUF.setModel(jComboUFModel);
				jComboUF.setBounds(126, 50, 284, 23);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Fecha");
				jLabel3.setBounds(51, 90, 68, 16);
				jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				Calendar fecha = Calendar.getInstance();
				calendario = new JDateChooser();
				calendario.setCalendar(fecha);
				getContentPane().add(calendario);
				calendario.setBounds(129, 89, 127, 23);
				
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Descripcion");
				jLabel4.setBounds(37, 124, 82, 16);
				jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				jEditorPane1 = new JEditorPane();
				getContentPane().add(jEditorPane1);
				jEditorPane1.setBounds(128, 124, 321, 99);
				jEditorPane1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5);
				jLabel5.setText("Urgencia");
				jLabel5.setBounds(49, 235, 70, 16);
				jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Registrar");
				btnAceptar.setBounds(178, 299, 92, 23);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						String urgencia = getUrgenciaSeleccionada();
						String consorcio = jComboConsorcios.getSelectedItem().toString();
						int uf = Integer.parseInt(jComboUF.getSelectedItem().toString());
						//java.util.Date fecha = new Date();
						//Calendar fecha = Calendar.getInstance();
																	
						Sistema.getInstancia().registrarReclamo(consorcio, calendario.getCalendar(), uf, jEditorPane1.getText(), urgencia);
						limpiaPantalla();
						
						
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				getContentPane().add(getJPanel1x());
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(281, 299, 92, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			pack();
			this.setSize(504, 371);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print("Error en la interface de Registrar Reclamo");
		}
	}
	
	
	private JPanel getJPanel1x() {
		if(jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setBounds(126, 235, 321, 35);
			jPanel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			jPanel1.add(getJRadioButton1());
			jPanel1.add(getJRadioButton2());
			jPanel1.add(getJRadioButton3());
		}
		return jPanel1;
	}
	
	private JRadioButton getJRadioButton1() {
		if(jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setText("Alta");
			jRadioButton1.setPreferredSize(new java.awt.Dimension(56, 20));
			jRadioButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					jRadioButton2.setSelected(false);
					jRadioButton3.setSelected(false);
				}
			});
		}
		return jRadioButton1;
	}
	
	private JRadioButton getJRadioButton2() {
		if(jRadioButton2 == null) {
			jRadioButton2 = new JRadioButton();
			jRadioButton2.setText("Media");
			jRadioButton2.setPreferredSize(new java.awt.Dimension(64, 20));
			jRadioButton2.setSelected(true);
			jRadioButton2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					jRadioButton1.setSelected(false);
					jRadioButton3.setSelected(false);
				}
			});
		}
		return jRadioButton2;
	}
	
	private JRadioButton getJRadioButton3() {
		if(jRadioButton3 == null) {
			jRadioButton3 = new JRadioButton();
			jRadioButton3.setText("Baja");
			jRadioButton3.setPreferredSize(new java.awt.Dimension(60, 20));
			jRadioButton3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					jRadioButton1.setSelected(false);
					jRadioButton2.setSelected(false);
				}
			});
		}
		return jRadioButton3;
	}

}
